package cn.lyh.iotmanage.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.lyh.iotmanage.dao.EmqClientsDao;
import cn.lyh.iotmanage.dao.EmqSessionsubDao;
import cn.lyh.iotmanage.service.EmqClientsDaoService;
import cn.lyh.iotmanage.service.EmqSessionSubService;
import cn.lyh.iotmanage.service.EmqTopicMsgDaoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author lyh
 * @version 1.0
 * @since 2021/5/15
 * 此控制类用于接收EMQX中规则引擎转发过来的消息
 */
@RestController
@RequestMapping("/emqx/")
@Slf4j
public class EMQXRuleEngineController {


	@Autowired
	EmqClientsDaoService clientsDaoService;

	@Autowired
	EmqSessionSubService sessionSubService;

	@Autowired
	EmqTopicMsgDaoService topicMsgDaoService;


	/**
	 * 设备上线转发消息接收接口
	 * @param params EMQ管理控制台转发过来的消息体
	 */
	//client_connected
	@PostMapping("client/connected")
	public void clientConnectedMsg(@RequestBody Map<String, Object> params) {
		log.info("connected params:{}",params);
		clientsDaoService.saveClientInfo(params);
	}

	/**
	 * 设备下线转发消息接收接口
	 * @param params EMQ管理控制台转发过来的消息体
	 */
	//client_disconnected
	@PostMapping("client/disconnected")
	public void disconnectedMsg(@RequestBody Map<String,Object> params){
		log.info("disconnected params:{}",params);
		clientsDaoService.saveDisconnectedInfo(params);
	}

	/**
	 * session_subscribed
	 * clientid,qos,timestamp,topic,username
	 * 监听设备订阅主题消息转发
	 * @param params EMQ传入的数据结构体
	 */
	@PostMapping("session/subscribed")
	public void subscribedMsg(@RequestBody Map<String,Object> params){
		log.info("subscribed params:{}",params);
		sessionSubService.saveSubscribedInfo(params);
	}

	@PostMapping("topic/topicmsg")
	public void topicMsgReceive(@RequestBody Map<String,Object> params){
		System.out.println("params = " + params);
		topicMsgDaoService.saveMassge(params);
	}

}
